package com.gg.zjj.controller;

import com.gg.common.pojo.MManufacture;
import com.gg.common.pojo.MProcedure;
import com.gg.zjj.pojo.DataAfterSubmission;
import com.gg.zjj.pojo.MManufactureView;
import com.gg.zjj.pojo.MProcedureModuleData;
import com.gg.zjj.service.InternalProductionAuditService;
import com.gg.zjj.util.Result;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author koco
 * 2022/10/25
 * 16:45
 */@Controller
@RequestMapping("/zjj/")
public class InternalProductionAuditController {

     @Resource
     private InternalProductionAuditService productionAuditService;

    @RequestMapping("queryTheProductionOperationToBeApproved.action")
    @ResponseBody
    private Result<List<MManufacture>> queryTheProductionOperationToBeApproved() {
        return Result.success(productionAuditService.queryTheProductionOperationToBeApprovedService());
    }

    @RequestMapping("queryingProductionProcessRecords.action")
    @ResponseBody
    private Result<MManufacture> queryingProductionProcessRecords(@RequestBody String val) {
        return Result.success(productionAuditService.queryingProductionProcessRecords(val));
    }

    @RequestMapping("queryAllIngProductionProcessRecords.action")
    @ResponseBody
    private Result<MManufactureView> queryAllIngProductionProcessRecords(@RequestBody String val) {
        return Result.success(productionAuditService.queryAllIngProductionProcessRecords(val));
    }

    @RequestMapping("approvalProcessSubmission.action")
    @ResponseBody
    private Result approvalProcessSubmission(@RequestBody String val) {
        return productionAuditService.approvalProcessSubmissionService(val);
    }

    @RequestMapping("submitTheLastProcess.action")
    @ResponseBody
    private Result submitTheLastProcess(@RequestBody String val) {
        try {
            productionAuditService.submitTheLastProcess(val);
            return Result.success();
        } catch (Exception e) {
            return Result.error("Error submitting");
        }
    }

    @RequestMapping("approveOperation.action")
    @ResponseBody
    private Result approveOperation(@RequestBody String val) {
        DataAfterSubmission dataAfterSubmission = (DataAfterSubmission) JSONObject.toBean(JSONObject.fromObject(val), DataAfterSubmission.class);
        List<MProcedureModuleData> procedureModuleDataList= new ArrayList<>();
        for (int i = 0; i < dataAfterSubmission.getModelData().size(); i++) {
            MProcedureModuleData mProcedureModuleData1 = (MProcedureModuleData) JSONObject.toBean(JSONObject.fromObject(dataAfterSubmission.getModelData().get(i)), MProcedureModuleData.class);
            procedureModuleDataList.add(mProcedureModuleData1);
        }
        dataAfterSubmission.setModelData(procedureModuleDataList);
        return productionAuditService.approveOperationService(dataAfterSubmission);
    }
}
